
use "/homeKRTK/health_shocks/data_for_main_regs_event_study_20231203.dta", clear
drop if control==1

append using "/homeKRTK/health_shocks/additional_data_for_IV_and_placebo_regs_20240416.dta"
replace treated=1 if d==2
drop if d==1 & control==0

append using "/homeKRTK/health_shocks/additional_data_for_all_regs_controls_d_distrib_as_treated_20250319.dta"



gen NONEMP = 1 if ABS==1
	recode NONEMP (.=0) if ABS==0
	
set seed 12345

keep if d<=6
*keep if d>=3 & d<=6
*keep if NONEMP !=.

gen month_shock = t-12*int(t/12)
	recode month_shock (0=12)
gen month_return = month_shock+d

*keep if treated == 1 | control == 1

forval i=1/8 {
	local j=`i'+8
	rename mean_fq`i'_w w_q`j'
	replace mean_fq`i'_FEj_imp =. if flag_missingFEj_fq`i'==1
	rename mean_fq`i'_FEj_imp FE_q`j'


}
forval i=1/8 {
	local j=8-`i'
	rename mean_lq`i'_w w_q`j'
	replace mean_lq`i'_FEj_imp =. if flag_missingFEj_lq`i'==1
	rename mean_lq`i'_FEj_imp FE_q`j'
}


* reshape *
capture drop FEj_t4 FEj_t5 FEj_t6 FEj_t7 FEj_t8 FEj_t9 FEj_t10 FEj_t11 FEj_t12 
capture drop lnw_restr_t4 lnw_restr_t5 lnw_restr_t6 lnw_restr_t7 lnw_restr_t8 lnw_restr_t9 lnw_restr_t10 lnw_restr_t11 lnw_restr_t12 

reshape long w_q FE_q, i(anon) j(relative_time)


recode teaor1_h1 (-1=99) (.=100)

cap drop date
gen date=t+(relative_time-8)*3
replace date=t+d+(relative_time-9)*3 if relative_time>=8

rename kor kor_orig

rename ev ev_orig
rename t t_orig
rename date t

gen ev=floor((t-1)/12)+2003
merge 1:1 anon t using "/homeProspSSD/Admin3/admin3_alap.dta", nogen keep(master match) keepusing(vallazon1 wtip1 wh1 feor1_h2 kor)
replace wh1=. if wh1>40
rename vallazon1 vallazon
merge m:1 vallazon ev using "/homeKRTK/health_shocks/firmquality.dta", nogen keep(master match)

merge 1:1 anon t using "/homePCIESSD800/Admin3/Output_data/Alap/admin3_passziv_kieg_H1_v1.dta", nogen keepusing(passziv_ho)
replace passziv_ho=0 if passziv_ho==.

by anon, sort: gegen full_d=min(d)
drop if full_d==. //drop anons who are never in our baseline sample

rename t date
rename t_orig t

by anon, sort: gegen full_t=min(t)

gen sl_relevant1_=passziv_ho if date>=full_t & date<=full_t+1
by anon, sort: gegen sl_relevant1=total(sl_relevant1_)
drop sl_relevant1_

drop if d==. //back to baseline+falsification sample
drop if d>6

keep if sl_relevant1>0 | control==1 //sample restriction!!!

cap drop after
gen after=0
replace after=1 if relative_time>=9 & relative_time<=12
replace after=2 if relative_time>=13 & relative_time<=16

gen tr_relative_time=treated*relative_time
cap drop rtdum*
tabulate tr_relative_time, generate(rtdum)

forval i=2/8 {
	replace rtdum`i'=rtdum`i'-rtdum1
}

xtset anon relative_time

//Covariates: ferfi kor occupation teaor1_h1
gen kor_orig2=kor_orig^2
replace teaor1_h1=100 if teaor1_h1==99 //merge missings

by vallazon, sort: gegen treated_ever=max(treated) //flag firms with a treated worker ever
drop if control==1 & treated_ever==1 & letszam<=100 //exclude controls with treated ever in the firm if firm size<=100

tabulate occupation, gen(docc)
tabulate teaor1_h1, gen(dind)

cap drop treated_pred
logit treated ferfi kor_orig kor_orig2 i.occupation i.teaor1_h1 
predict treated_pred, pr

cap drop pr_weight
gen pr_weight=1 if treated==1
replace pr_weight=treated_pred/(1-treated_pred) if treated==0

xtset anon relative_time

****Input for Table A3

log using /homeKRTK/health_shocks/EventStudy/log_EventStudy_match_Marc2025.log, append

tabstat kor ferfi docc* dind*, by(treated) 
tabstat kor ferfi docc* dind* [aw=pr_weight], by(treated) 

log close

*Weighted regression - Input for Figure A5 - saved in "$folder\Matching_Marc2025.xlsx"
log using /homeKRTK/health_shocks/EventStudy/log_EventStudy_match_Marc2025.log, append

xtreg w_q i.date i.relative_time rtdum2-rtdum16 if ev_orig>=2012 & letszam~=. & FE_q~=. [aw=pr_weight], fe rob
lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8

xtreg w_q i.date treated##ib0.after if  ev_orig>=2012 & letszam~=. & FE_q~=. [aw=pr_weight], fe rob

log close


**Matching across firms

compress
save "/homeKRTK/health_shocks/matching_data_Marc2025.dta", replace

sort anon t
cap drop treated_pred
logit treated ferfi kor_orig kor_orig2 i.occupation i.teaor1_h1 
predict treated_pred, pr

capture drop int_pred
gen int_pred = int(treated_pred*10000)

preserve 
keep if treated==1
keep if relative_time==7
keep treated anon int_pred vallazon
rename anon anon_treated
rename vallazon vallazon_treated
sort int_pred
save "/homeKRTK/health_shocks/treated_to_be_joined_Marc2025.dta", replace
restore

sort anon t
keep if treated==0
keep if relative_time==7

keep treated anon int_pred vallazon

gen control=1
rename anon anon_control
rename vallazon vallazon_control

sort int_pred
joinby int_pred using "/homeKRTK/health_shocks/treated_to_be_joined_Marc2025.dta"

drop if vallazon_treated==vallazon_control
* keep 5 matches *
set seed 98034
generate random = runiform()
sort anon_treated random
egen seq = seq(), by(anon_treated)
keep if seq<=1

preserve
duplicates drop anon_treated, force
rename anon_treated anon_1
save "/homeKRTK/health_shocks/treated_joined_Marc2025.dta", replace
restore 

preserve
duplicates drop anon_control, force
rename anon_control anon_0
save "/homeKRTK/health_shocks/control_joined_Marc2025.dta", replace
restore 

use "/homeKRTK/health_shocks/matching_data_Marc2025.dta", clear
gen anon_1 = anon
merge m:1 anon_1 using  "/homeKRTK/health_shocks/treated_joined_Marc2025.dta" 

drop if _m==2
gen matched_treated = 1 if _m==3
drop _m

gen anon_0 = anon
merge m:1 anon_0 using  "/homeKRTK/health_shocks/control_joined_Marc2025.dta" 

drop if _m==2
gen matched_control = 1 if _m==3
drop _m

***Input for Figure A5 - saved in "$folder\Matching_Marc2025.xlsx"

log using /homeKRTK/health_shocks/EventStudy/log_EventStudy_match_Marc2025.log, append

xtreg w_q i.date i.relative_time rtdum2-rtdum16 if ev_orig>=2012 & letszam~=. & w_q~=. & FE_q~=. & (matched_treated == 1 | matched_control == 1), fe rob
lincom -rtdum2-rtdum3-rtdum4-rtdum5-rtdum6-rtdum7-rtdum8

xtreg w_q i.date treated##ib0.after if  ev_orig>=2012 & letszam~=. & FE_q~=. & (matched_treated == 1 | matched_control == 1), fe rob

log close
